package day05;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("*.cart")
public class CartServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String path = request.getServletPath();

        if ("/add.cart".equalsIgnoreCase(path)) {
            add(request, response);
        }


        if ("/clear.cart".equalsIgnoreCase(path)) {
            clear(request, response);
        }



        if ("/removeByProductId.cart".equalsIgnoreCase(path)) {
            removeByProductId(request, response);
        }


        if ("/removeByProductIds.cart".equalsIgnoreCase(path)) {
            removeByProductIds(request, response);
        }

        if ("/modify.cart".equalsIgnoreCase(path)) {
            modify(request, response);
        }
    }

    protected void modify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("CartServlet.modify()");

        Integer productId=Integer.parseInt(request.getParameter("productId"));
        Integer num=Integer.parseInt(request.getParameter("num"));

        Cart cart=(Cart) request.getSession().getAttribute("cart");

        cart.modify(productId,num);

        response.sendRedirect(request.getContextPath()+"/shop/cart.jsp");
    }


    protected void removeByProductIds(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("CartServlet.removeByProductIds()");

        String[] productIds=request.getParameterValues("productIds");

        List<Integer> ids=new ArrayList<Integer>();

        if(productIds!=null){
            for (String productId : productIds) {
                ids.add(Integer.parseInt(productId));
            }
        }


        Cart cart=(Cart) request.getSession().getAttribute("cart");

        cart.removeByProductIds(ids);

        response.sendRedirect(request.getContextPath()+"/shop/cart.jsp");
    }


    protected void removeByProductId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("CartServlet.removeByProductId()");

        Integer productId=Integer.parseInt(request.getParameter("productId"));

        Cart cart=(Cart) request.getSession().getAttribute("cart");

        cart.removeByProductId(productId);

        response.sendRedirect(request.getContextPath()+"/shop/cart.jsp");

    }


    protected void clear(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("CartServlet.clear()");

        Cart cart=(Cart) request.getSession().getAttribute("cart");

        cart.clear();

        response.sendRedirect(request.getContextPath()+"/shop/cart.jsp");
    }


    protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("CartServlet.add()");

        Integer productId=Integer.parseInt(request.getParameter("productId"));

        Cart cart= (Cart) request.getSession().getAttribute("cart");

        cart.add(productId);

        // request.getSession().setAttribute("cart",cart);

        response.sendRedirect(request.getContextPath()+"/list.product");

    }

}
